package com.jf.net.interceptor

import com.jf.net.utils.NetUtils
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response

/**
 * RequestInterceptor
 * @author JackyFlame
 * @time 2023/10/20 14:50
 * @description
 */
class CacheInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        val response = chain.proceed(request)
        if (NetUtils.isConnected) {
            val cacheControl: String = request.cacheControl.toString()
            if (cacheControl.isNotEmpty()) {
                return response.newBuilder()
                    .removeHeader("Pragma")
                    .header("Cache-Control", cacheControl)
                    .build()
            }
        }
        return response
    }
}